package com.atguigu.web;

import com.atguigu.Bean.C_book;
import com.atguigu.Bean.C_page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet{
    private BookService bs=new BookServiceImpl();
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1、获取请求的参数
        C_book cb=new C_book();
        WebUtils.copyParamToBean(req.getParameterMap(),cb);
//        2、调用BookService.addBook()保存图书
        bs.addBook(cb);
//        3、跳转到图书列表页面
        //req.getRequestDispatcher("/manager/bs?action=list").forward(req,resp);
        //如果这样操作会出现bug:刷新一次数据就又提交一次【表单重复提交】
        resp.sendRedirect(req.getContextPath()+"/manager/bs?action=page&pageNo="+req.getParameter("pageNo"));
        //req.getContextPath()表示“/工程名”
/**用请求重定向可以防止表单重复提交
 * 因为请求重定向发送两次请求，但刷新时则执行最后一次请求，
 * 即前往/manager/bs并执行list方法；
 * 而请求转发只有一次请求，即通过servlet程序添加数据，
 * 并且前往/manager/bs并执行list方法；
 */
//【登陆页面可以表单重复提交，这里添加书则不可以】
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1、获取请求参数id
        int id = WebUtils.parseInt(req.getParameter("id"),-1);
//        2、调用BookService.deleteBookBuId();删除图书
        bs.deleteBookBYId(id);
//        3、请求重定向返回修改页面/manager/bs?action=list
        resp.sendRedirect(req.getContextPath()+"/manager/bs?action=page&pageNo="+req.getParameter("pageNo"));
    }


    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取请求的参数，封装成为C_book对象
        C_book c=new C_book();
        WebUtils.copyParamToBean(req.getParameterMap(),c);
        //2、调用BookService实例对象.updateBook();修改图书
        bs.updateBook(c);
        //3、重定向回图书列表管理页面/manager/bs?action=list
        resp.sendRedirect(req.getContextPath()+"/manager/bs?action=page&pageNo="+req.getParameter("pageNo"));
    }
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取请求的图书的编号
        int id = WebUtils.parseInt(req.getParameter("id"),-1);
        //2、调用BookService实例对象.queryVookById查询图书
        C_book book = bs.queryBookById(id);
        //3、保存到图书到Request域中
        req.setAttribute("book",book);
        //4、请求转发到pages/ManagerPages/c_manager_book_edit.jsp页面
        req.getRequestDispatcher("/pages/ManagerPages/c_manager_book_edit.jsp?pageNo="+req.getParameter("pageNo")).forward(req,resp);
    }


    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、通过BookService查询全部图书
        List<C_book> c_books = bs.queryBooks();
        //2、把全部图书保存到Request域中
        req.setAttribute("books",c_books);
        //3、请求转发到/pages/ManagerPages/c_game_books_manager.jsp
        RequestDispatcher rd = req.getRequestDispatcher("/pages/ManagerPages/c_game_books_manager.jsp");
        rd.forward(req,resp);
    }

    /**
     * 处理分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1、获取请求的参数 pageNo和pageSize。
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), C_page.PAGE_SIZE);
//        2、调用BookService.page(pageNo,pageSize)返回Page对象
        C_page<C_book> page = bs.page(pageNo, pageSize);
//        2.5、设置翻页操作所用的请求地址
        page.setUrl("manager/bs?action=page");
//        3、保存到Request域中
        req.setAttribute("page",page);
//        4、请求转发到/pages/ManagerPages/c_game_books_manager.jsp
        req.getRequestDispatcher("/pages/ManagerPages/c_game_books_manager.jsp").forward(req,resp);
    }
}
